package com.myx.typhoon.web.bussiness.controller;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myx.typhoon.business.domain.Comment;
import com.myx.typhoon.business.domain.User;
import com.myx.typhoon.business.service.CommentService;
import com.myx.typhoon.sys.service.UserService;
import com.myx.typhoon.web.bussiness.domian.result.RequestPageVo;
import com.myx.typhoon.web.security.SecurityUtil;
import com.myx.typhoon.web.util.NetworkUtil;

/**  
 * @author myx
 * @createTime 2016年1月21日 下午4:10:25  
 * 
 */
@Controller
@RequestMapping("/comment")
public class CommentController {
	
	@Resource
	private CommentService commentService;
	
	@Resource
	private UserService userService;
	
	/**
	 * 获取评论列表
	 * @param articleId
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/getCommentsList")
	public @ResponseBody RequestPageVo<Comment> getCommentsList(
			@RequestParam("articleId") Long articleId,
			@RequestParam(value = "pageNum",defaultValue = "1",required = false) int pageNum ,
			@RequestParam(value = "pageSize",defaultValue = "10",required = false) int pageSize) {
		RequestPageVo<Comment> page = new RequestPageVo<Comment>();
		int start = (pageNum-1)*pageSize;
		page = commentService.findCommentByArticleId(articleId, start, pageSize);
		return page;
	}
	
	/**
	 * 发表评论
	 * @param id
	 * @return
	 */
	@RequestMapping("/sendComment")
	public String sendComment(@ModelAttribute Comment comment,
			HttpServletRequest request) {
		//RequestResultVo<Comment> result = new RequestResultVo<Comment>();
		User user = SecurityUtil.getCurrentUser();
		String ip = NetworkUtil.getIpAddress(request);
		comment.setIp(ip);
		commentService.saveComment(comment, user.getUserId());
		return "redirect:/article/getArticleDetial?id="+comment.getArticleId();
	}
}
